import Element from '../base/Element'
import Word from './word/word'
import FutureWorld from './futureWorld/index'

export default class World extends Element {
  constructor() {
    super()
    this.wordList = []
    this.wordDiff = []
    this.index = 0
    this.timer = null
    this.matrixArr = []
    this.wordArr = [
      '金',
      '木',
      '水',
      '火',
      '土',
      '乾',
      '坤',
      '震',
      '巽',
      '坎',
      '艮',
      '离',
      '兑',
      '甲',
      '乙',
      '丙',
      '丁',
      '戊',
      '己',
      '庚',
      '辛',
      '壬',
      '癸',
      '子',
      '丑',
      '寅',
      '卯',
      '辰',
      '巳',
      '午',
      '未',
      '申',
      '酉',
      '戌',
      '亥',
      '甲',
      '卯',
      '乙',
      '辰',
      '巽',
      '巳',
      '丙',
      '午',
      '丁',
      '未',
      '坤',
      '申',
      '庚',
      '酉',
      '辛',
      '戍',
      '干',
      '亥',
      '壬',
      '子',
      '癸',
      '丑',
      '艮',
      '寅',
    ]
    this.resource.on('ready', () => {
      // this.word = new Word({ text: '土', font: 'ZeroGo' })
      // this.word1 = new Word({
      //   text: '乾',
      //   font: 'zhiMangXing', //ZeroGo
      //   position: [0, 20, 0],
      //   textFrontColor: '#FFFFFF',
      //   textBackColor: '#fff000',
      //   debug: true,
      // })
      // console.log(this.resource.items)
      // this.loop()
      this.futureWorld = new FutureWorld()
      // this.initDebugCamera()
      // this.initMatrix()
      // this.initWords()
    })
  }
  initMatrix() {
    for (let i = 1; i < 10; i++) {
      let bool = Math.random() < 0.5
      let meshArr = [[0, 20 * i]]
      for (let j = 1; j < 10; j++) {
        let arr = [20 * j, 20 * i]
        meshArr.push(arr)
      }
      this.matrixArr.push(meshArr)
    }
    console.log('initMatrix', this.matrixArr)
  }

  initWords() {
    this.matrixArr.forEach((item, index) => {
      item.forEach((position) => {
        const [x, y] = position
        let textIndex = parseInt(this.wordArr.length * Math.random())
        let text = this.wordArr[textIndex]
        let word = new Word({ text: text, font: 'zhiMangXing', position: [x, y, 0] })
        this.wordList.push(word)
      })
    })
  }

  loop() {
    if (this.wordList.length >= 7) {
      this.wordList[0].deleteWord()
      this.wordList.splice(0, 1)
    }
    if (this.index < this.wordArr.length) {
      let text = this.wordArr[this.index]
      let x = Math.random() > 0.5 ? -(Math.random() * 100) : Math.random() * 100
      let y = Math.random() * 100
      let position = [x, y, 0]
      let word = new Word({ text: text, font: 'zhiMangXing', position: position })
      this.wordList.push(word)
      this.index++
    } else {
      this.index = 0
    }
    this.timer = setTimeout(() => {
      this.loop()
    }, 1000)
  }

  initDebugCamera() {
    const self = this
    const ui = this.debug.gui
    const groupFolder = ui.addFolder('相机')
    groupFolder.add(this.camera.instance.position, 'x').min(-3000).max(3000).step(100).name('cameraPositionX')
    groupFolder.add(this.camera.instance.position, 'y').min(-3000).max(3000).step(100).name('cameraPositionY')
    groupFolder.add(this.camera.instance.position, 'z').min(-3000).max(3000).step(100).name('cameraPositionZ')
    groupFolder.add(this.camera.instance, 'near').min(0.1).max(10).step(0.1).name('cameraNear')
    groupFolder.add(this.camera.instance, 'far').min(100).max(20000).step(100).name('cameraFar')
    groupFolder.add(this.camera.instance, 'zoom').min(0.1).max(10).step(0.1).name('cameraZoom')
    groupFolder.add(this.camera.instance, 'zoom').min(0.1).max(10).step(0.1).name('cameraZoom')
    // 是否需要辅助线
    // let cameraHelp = new (function () {
    //   this.相机辅助线 = true
    // })()
    // groupFolder.add(cameraHelp, '相机辅助线').onChange((e) => {
    //   console.log(e)
    //   if (!e) {
    //     self.camera.removeCameraHelper()
    //   } else {
    //     self.camera.createCameraHelper()
    //   }
    // })
  }

  update() {
    this.wordList.forEach((item) => {
      item.update()
    })
    this.futureWorld && this.futureWorld.update()
  }
  resize() {}
}
